package com.itheima.controller;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.*;
import com.itheima.po.Comment;
import com.itheima.po.User;
import com.itheima.service.BookService;
import com.itheima.service.CommentService;
@Controller
public class CommentController {
	@Autowired
	private CommentService commentService;
	/*
	 * 创建评论
	 *	
	 */
	@RequestMapping(value="/addComment.action")
	@ResponseBody
	public JSONObject createComment(Comment comment,HttpSession session) {
		String userid = ((User)session.getAttribute("USER_SESSION")).getUserid();
		comment.setUserid(userid);
		Timestamp timeStamp = new Timestamp(System.currentTimeMillis());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		comment.setTime(df.format(timeStamp));
		comment.setState("n");
		int rows = commentService.addComment(comment);	//受影响的行数
		JSONObject json=new JSONObject();
		if(rows>0) {
			json.put("status", "succeed");
			json.put("commentid",comment.getCommentid());	//返回评论表中的主键
		}
		else
			json.put("status", "failed");
		return json;
		
	}
	/*
	 * 获取图书全部已审核评论
	 */
	@RequestMapping(value="/getBookComments.action")
	@ResponseBody
	public JSONArray getBookComments(int bookid) {
		List<Comment> comments = commentService.getBookComments(bookid);
		JSONArray jarray = JSONArray.parseArray(JSON.toJSONString(comments));
		return jarray;
	}
	/*
	 * 获取全部未审核评论
	 */
	@RequestMapping(value="/getUncheckedComments.action")
	@ResponseBody
	public JSONArray getUncheckedComments() {
		List<Comment> comments = commentService.getUncheckedComments();
		JSONArray jarray = JSONArray.parseArray(JSON.toJSONString(comments));
		return jarray;
	} 
	/*
	 * 审核某一评论
	 */
	@RequestMapping(value="/checkComment.action")
	@ResponseBody
	public JSONObject checkComment(int commentid) {
		JSONObject json = new JSONObject();
		int rows = commentService.checkComment(commentid);
		if(rows>0)
			json.put("status", "succeed");
		else
			json.put("status", "failed");
		return json;
	}
	/*
	 * 用户获取其所有评论
	 */
	@RequestMapping(value="/getUserComments.action")
	@ResponseBody
	public JSONObject getUserComments(HttpSession session) {
		String userid = ((User)session.getAttribute("USER_SESSION")).getUserid();
		List<Comment> comments = commentService.getUserComments(userid);
		JSONArray jarray = JSONArray.parseArray(JSON.toJSONString(comments));
		JSONObject json=new JSONObject();
		json.put("comments", jarray);
		return json;
	}
	/*
	 * 用户删除其某一评论
	 */
	@RequestMapping(value="/deleteComment.action")
	@ResponseBody
	public JSONObject deleteComment(int commentid) {
		JSONObject json = new JSONObject();
		int rows = commentService.deleteComment(commentid);
		if(rows>0)
			json.put("status", "succeed");
		else
			json.put("status", "failed");
		return json;
		
	}
	
	/*
	 * 管理员获取所有已审核评论
	 */
	@RequestMapping(value="/getCheckedComments.action")
	@ResponseBody
	public JSONObject getCheckedComments() {
		JSONObject json = new JSONObject();
		List<Comment> comments = commentService.getCheckedComments();
		JSONArray jarray = JSONArray.parseArray(JSON.toJSONString(comments));
		json.put("comments", jarray);
		return json;
		
	}
}
